** creating psa5 (system support)
egen missing = rowmiss(b1 b2 b3 b4 b6)
egen b1to6 = rowmean(b1 b2 b3 b4 b6) 
	replace b1to6=. if missing>2
recode b1to6 (1/1.79=0)(1.8/3.29=25)(3.3/4.79=50)(4.8/6.29=75)(6.3/7=100), gen(psa5)


** creating edr (levels of education) 
gen edr=.
egen pais1 = cut(ed) if wave<=2018 & pais==1, at(0,1,7,13,30) icode
egen pais2 = cut(ed) if wave<=2018 & pais==2, at(0,1,7,13,30) icode
egen pais3 = cut(ed) if wave<=2018 & pais==3, at(0,1,7,13,30) icode
egen pais4 = cut(ed) if wave<=2018 & pais==4, at(0,1,7,13,30) icode
egen pais5 = cut(ed) if wave<=2018 & pais==5, at(0,1,7,12,30) icode  
egen pais6 = cut(ed) if wave<=2018 & pais==6, at(0,1,7,13,30) icode
egen pais7 = cut(ed) if wave<=2018 & pais==7, at(0,1,7,13,30) icode
egen pais8 = cut(ed) if wave<=2018 & pais==8, at(0,1,6,12,30) icode
egen pais9 = cut(ed) if wave<=2018 & pais==9, at(0,1,7,13,30) icode
egen pais10 = cut(ed) if wave<=2018 & pais==10, at(0,1,7,13,30) icode
egen pais11 = cut(ed) if wave<=2018 & pais==11, at(0,1,7,12,30) icode
egen pais12 = cut(ed) if wave<=2018 & pais==12, at(0,1,7,13,30) icode
egen pais13 = cut(ed) if wave<=2018 & pais==13, at(0,1,7,13,30) icode
egen pais14 = cut(ed) if wave<=2018 & pais==14, at(0,1,7,13,30) icode
egen pais15 = cut(ed) if wave<=2018 & pais==15, at(0,1,7,13,30) icode
egen pais16 = cut(ed) if wave<=2018 & pais==16, at(0,1,7,12,30) icode
egen pais17 = cut(ed) if wave<=2018 & pais==17, at(0,1,8,13,30) icode
egen pais21 = cut(ed) if wave<=2018 & pais==21, at(0,1,9,13,30) icode
egen pais22 = cut(ed) if wave<=2018 & pais==22, at(0,1,8,15,30) icode
egen pais23 = cut(ed) if wave<=2018 & pais==23, at(0,1,7,12,30) icode
egen pais24 = cut(ed) if wave<=2018 & pais==24, at(0,1,7,12,30) icode
egen pais25 = cut(ed) if wave<=2018 & pais==25, at(0,1,6,13,30) icode
egen pais26 = cut(ed) if wave<=2018 & pais==26, at(0,1,7,13,30) icode
egen pais27 = cut(ed) if wave<=2018 & pais==27, at(0,1,7,14,30) icode
egen pais28 = cut(ed) if wave<=2018 & pais==27, at(0,1,7,13,30) icode
egen pais29 = cut(ed) if wave<=2018 & pais==29, at(0,1,7,13,30) icode
egen pais30 = cut(ed) if wave<=2018 & pais==30, at(0,1,8,13,30) icode
egen pais31 = cut(ed) if wave<=2018 & pais==31, at(0,1,8,13,30) icode
egen pais32 = cut(ed) if wave<=2018 & pais==32, at(0,1,8,13,30) icode
egen pais33 = cut(ed) if wave<=2018 & pais==33, at(0,1,8,13,30) icode
egen pais34 = cut(ed) if wave<=2018 & pais==34, at(0,1,8,13,30) icode
egen pais35 = cut(ed) if wave<=2018 & pais==35, at(0,1,8,13,30) icode
 
forval i=1/35 {
      cap replace edr=pais`i' if pais==`i' & wave<=2018
      cap drop pais`i'
}



**  creating quintall (levels of wealth) 
cap ssc install sumdist
gen quintall=.
levelsof wave , local(W)
cap recode r16 (.c=0), gen(tempr16)
foreach w of local W {
    if `w'==2004 levelsof pais if wave==`w' & inrange(pais, 1, 10), local(K)
    if `w'==2006 levelsof pais if wave==`w' & inrange(pais, 1, 13) | pais==16 | inrange(pais, 18, 24), local(K)
    if `w'==2008 levelsof pais if wave==`w' & inrange(pais, 1, 24) | pais==26 , local(K)
    if inrange(`w', 2010, 2018) levelsof pais if wave==`w' & pais<40, local(K)
    foreach k of local K {
        qui levelsof ur if pais==`k' & wave==`w', local(U)
        foreach u of local U {
            if `w'==2004 qui pca r1 r3 r4 r5 r6 r7 r12 if pais==`k' & ur==`u' & wave==`w'
            if inlist(`w', 2006, 2008) qui pca r1 r3 r4 r4a r5 r6 r7 r8 r12 r14 r15 if pais==`k' & ur==`u' & wave==`w'
            if `w'==2010 qui pca r1 r3 r4 r4a r5 r6 r7  r8 r12 r14 r15 r16 r18 if pais==`k' & ur==`u' & wave==`w'
            if `w'==2012 qui pca r1 r3 r4 r4a r5 r6 r7 r8 r12 r14 r15 r16 if pais==`k' & ur==`u' & wave==`w'
            if inrange(`w', 2014, 2018) qui pca r1 r3 r4 r4a r5 r6 r7 r8 r12 r14 r15 tempr16 r18 if pais==`k'& ur==`u' & wave==`w'
            qui predict _f1 if pais==`k' & ur==`u' & wave==`w'
            qui sumdist _f1 if !mi(_f1), n(5) qgp(_quint)
            qui replace quintall=_quint if pais==`k' & ur==`u' & wave==`w'
            drop _quint _f1
        }
    }
}
cap drop tempr16
